// D3D9VertexBuffer.cpp

#include "stdafx.h"

#include "..\Core\core.h"

#include "D3D9VertexBuffer.h"

//////////////////////////////////////////////////////////////////////////
CD3D9VertexBuffer::CD3D9VertexBuffer(size_t vertexSize, size_t numVertices, DWORD dwUsage, LPDIRECT3DDEVICE9 pDev)
{
	HRESULT hr = pDev->CreateVertexBuffer(vertexSize * numVertices, 
		dwUsage,
		0, 
		D3DPOOL_DEFAULT, 
		&m_pVertexBuffer, 
		NULL);

	if (FAILED(hr))
	{
		// msg failed to create vertex buffer.
	}
}

CD3D9VertexBuffer::~CD3D9VertexBuffer()
{
	SAFE_RELEASE(m_pVertexBuffer);
}

//////////////////////////////////////////////////////////////////////////
IDirect3DVertexBuffer9* CD3D9VertexBuffer::GetBuffer(void) const
{
	return m_pVertexBuffer;
}

//////////////////////////////////////////////////////////////////////////
void CD3D9VertexBuffer::write(size_t offset, size_t length, const void *pSource, bool bDiscardWholeBuffer)
{
	void* pDst = NULL;
	HRESULT hr = m_pVertexBuffer->Lock(static_cast<UINT>(offset), static_cast<UINT>(length), &pDst, D3DLOCK_DISCARD);

	if (FAILED(hr))
	{
		return;
	}

	memcpy(pDst, pSource, length);
	m_pVertexBuffer->Unlock();
}